/*
 * $Id$
 */
package py.windmill.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import py.windmill.resolution.Forward;
import py.windmill.resolution.Redirect;
import py.windmill.resolution.Tiles;
import py.windmill.resolution.TilesMap;

/**
 *
 * @author myang
 */
public class Route {

    private final SimpleUriTemplate _uriTemplate;
    private final Action _action;
    private final List<Resolution> _resolutions = new ArrayList();

    /**
     * 
     * @param action 
     */
    public Route(final Action action) {
        this(action.boundTo(), action);
    }

    /**
     * 
     * @param uri
     * @param action 
     */
    public Route(final String uri, final Action action) {
        _uriTemplate = new SimpleUriTemplate(uri);
        _action = action;
        _resolutions.addAll(_action.produces());
    }

    /**
     * 
     * @param uri
     * @param target
     * @return 
     */
    public static Route redirect(final String uri, final String target) {
        return new Route(simpleAction(uri, new Redirect(target)));
    }

    /**
     *
     * @param uri
     * @param target
     * @return
     */
    public static Route forward(final String uri, final String target) {
        return new Route(simpleAction(uri, new Forward(target)));
    }

    /**
     * 
     * @param uri
     * @param key
     * @param value
     * @return 
     */
    public static Route tiles(final String uri, final String key, final Object value) {
        return tiles(uri, new TilesMap(key, value));
    }

    /**
     * 
     * @param uri
     * @param tm
     * @return 
     */
    public static Route tiles(final String uri, final TilesMap tm) {
        return new Route(simpleAction(uri, new Tiles() {
            @Override
            public TilesMap prepare(final Request req, final Object val) {
                return tm;
            }
        }));
    }
    /**
     * 
     * @param uri
     * @param resolution
     * @return 
     */
    public static Action simpleAction(final String uri, final Resolution resolution) {
        return new ActionSupport() {
            @Override
            public List<Resolution> produces() {
                return produces(resolution);
            }

            @Override
            public String boundTo() {return uri; }

            @Override
            public Object onGet(final Request req) { return null; }
        };
    }

    /**
     * 
     * @return 
     */
    public Action action() {
        return _action;
    }

    /**
     * 
     * @return 
     */
    public String path() {
        return _uriTemplate.path();
    }

    /**
     * 
     * @return 
     */
    public boolean containsPathParameters() {
        return _uriTemplate.params().size() != 0;
    }

    /**
     * 
     * @param req
     * @return 
     */
    public Resolution resolution(final Request req) {
        for (final Resolution resolution : _resolutions) {
            if (resolution.isAcceptedBy(req)) {
                return resolution;
            }
        }
        return null;
    }

    /**
     * 
     * @param uri
     * @return 
     */
    public Map<String, String> parse(final String uri) {
        return _uriTemplate.parse(uri);
    }
}
